package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.AgentTypeVoAndCode;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.detailagentTypeBean;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.detailagentTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.invite_code;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;

/**
 * 代理类型详情 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_detailagentType {
	private static detailagentTypeBean bean;
	private static detailagentTypeCall call;
	DefGodMchWapper DefGodMchWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理类型详情");
	}

	@Before
	public void caseUp() {
		bean = new detailagentTypeBean();
		call = new detailagentTypeCall();
		DefGodMchWapper = new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * @param UsedAgent
	 *            验证使用人数
	 * @throws InterruptedException
	 */
	private void VerifyDb(Integer UsedAgent) throws InterruptedException {
		AgentTypeVoAndCode r = call.getResult();
		agent_type dbat = DefGodMchWapper.selectAgentType(bean.getMchId(), bean.getAgentType()).get(0);
		List<invite_code> dbcodes = DefGodMchWapper.selectInviteCode(bean.getMchId(), bean.getAgentType()).stream().filter(ic -> ic.getState() == 0).collect(Collectors.toList());
		Logger.verifyEquals(dbat.getAgentType(), r.getAgentType(), "验证AgentType");
		Logger.verifyEquals(dbat.getRemark(), r.getRemark(), "验证Remark");
		Logger.verifyEquals(dbat.getSeqNo(), r.getSeqNo(), "验证SeqNo");
		Logger.verifyEquals(dbat.getTypeTitle(), r.getTitle(), "验证Title");
		Logger.verifyEquals(dbat.getDiscount(), r.getDiscount(), "验证Discount");//新增
		Logger.verifyEquals(UsedAgent, r.getUsedAgent(), "验证UsedAgent");
		Logger.verifyEquals(dbcodes.size(), r.getCodeList().getTotal(), "验证size");
		r.getCodeList().getCodes().forEach(code -> {
			invite_code dbcode = dbcodes.stream().filter(dbc -> dbc.getInviteCode().equals(code.getCode())).findFirst().get();
			// Logger.verifyEquals(dbcode.getCodeRole(), , Description)
			// 有ExpireTime代表一次性的2 没有代表永久 1
			Logger.verifyEquals(dbcode.getExpireTime() == null ? 1 : 2, code.getCodeType(), "验证codetype");
			Logger.verifyEquals(dbcode.getRoleType(), code.getAgentType(), "验证AgentType");
			Logger.verifyEquals(dbat.getTypeTitle(), code.getAgentTypeTitle(), "验证AgentTypeTitle");
			
			
		});
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_detailagentType_001() {
		Logger.start(true, "代理类型详情,验证商户id和代理类型Id同时存在-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();

			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_002() {
		Logger.start(true, "代理类型详情,验证商户id与代理类型Id不匹配-返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(ccbean.getMchId() );
			bean.setAgentType(ccbean.getRoleType()-1);
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_003() {
		Logger.start(true, "代理类型详情,验证商户id不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(1);
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_004() {
		Logger.start(true, "代理类型详情,验证商户id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			// bean.setMchId(ccbean.getMchId()+1);
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_005() {
		Logger.start(true, "代理类型详情,验证代理类型Id不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(-1);
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_006() {
		Logger.start(true, "代理类型详情,验证代理类型Id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(ccbean.getMchId());
			// bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_007() {
		Logger.start(true, "代理类型详情,验证pageindex为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(ccbean.getMchId());
			 bean.setAgentType(ccbean.getRoleType());
//			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_008() {
		Logger.start(true, "代理类型详情,验证pageSize为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			bean.setMchId(ccbean.getMchId());
			 bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
//			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证没有返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_detailagentType_009() {
		Logger.start(true, "代理类型包含邀请码查询成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_detailagentType_010() {
		Logger.start(true, "代理类型包含多个邀请码查询成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setIsPermanently(false);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			ccbean.setIsPermanently(true);
			String code2 = GodmchManageCallService.createCodeCall(ccbean).getCode();
			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_detailagentType_011() {
		Logger.start(true, "代理类型包含邀请码查询成功-有人使用邀请码-code只有一次有效不展示");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setIsPermanently(false);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			ccbean.setIsPermanently(true);
			String code2 = GodmchManageCallService.createCodeCall(ccbean).getCode();
			// code 只有一次有效 用了后只展示一个code
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);

			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_detailagentType_012() {
		Logger.start(true, "代理类型包含多个邀请码分页查询成功");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setIsPermanently(false);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			ccbean.setIsPermanently(true);
			String code2 = GodmchManageCallService.createCodeCall(ccbean).getCode();
			bean.setMchId(ccbean.getMchId());
			bean.setAgentType(ccbean.getRoleType());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}